package com.yijushequ.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yijushequ.dao.ModelDao;
import com.yijushequ.domain.BaseUser;
import com.yijushequ.domain.Pagination;
import com.yijushequ.utils.Constants;

import java.io.*;
public class BasicAction extends ActionSupport {
    public static final String BASIC_PACKAGE = "basic-package";
    public static final String BASE_NAME_SPACE = "/";
    public static final String RESULT_JSON = "json";

    public static final String RESULT_NAME_TILES = "tiles";
    public static final String RESULT_NAME_REDIRECT_ACTION = "redirectAction";
    public static final String RESULT_CHAIN_ACTION = "chain";

    private Pagination pagination = new Pagination();
    private ModelDao modelDao;

    protected static void copy(File src, File dst) {

        if (!dst.getParentFile().exists()) {
            dst.getParentFile().mkdirs();
        }

        try {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = new BufferedInputStream(new FileInputStream(src), Constants.BUFFER_SIZE);
                out = new BufferedOutputStream(new FileOutputStream(dst), Constants.BUFFER_SIZE);
                byte[] buffer = new byte[Constants.BUFFER_SIZE];
                while (in.read(buffer) > 0) {
                    out.write(buffer);
                }
            } finally {
                if (null != in) {
                    in.close();
                }
                if (null != out) {
                    out.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    protected  String getSessionOpenId(){
        if (ActionContext.getContext().getSession().get(Constants.SESSION_OPEN_ID) != null)
            return (String) ActionContext.getContext().getSession().get(Constants.SESSION_OPEN_ID);
        return null;
    }

    protected  Integer getSessionUserId(){
        if (ActionContext.getContext().getSession().get(Constants.SESSION_USER_ID) != null)
            return (Integer) ActionContext.getContext().getSession().get(Constants.SESSION_USER_ID);
        return null;
    }

     protected Integer getSessionCommunity(){
        return 1;
     }


    protected void removeSession() {
        ActionContext.getContext().getSession().clear();
    }

    public ModelDao getModelDao() {
        return modelDao;
    }

    public void setModelDao(ModelDao modelDao) {
        this.modelDao = modelDao;
    }

    protected void setUserToSession(BaseUser user) {
        ActionContext.getContext().getSession().put(Constants.SESSION_USER_ID, user.getId());
        ActionContext.getContext().getSession().put(Constants.SESSION_USER_NICKNANE, user.getNickName());
        ActionContext.getContext().getSession().put(Constants.SESSION_OPEN_ID, user.getOpenId());
        ActionContext.getContext().getSession().put(Constants.SESSION_FEATURE_URL, user.getFigureUrl());
//        ActionContext.getContext().getSession().put(Constants.SESSION_COMMUNITY_ID, this.getSessionCommunity());

    }


    public Pagination getPagination() {
        return pagination;
    }

    public void setPagination(Pagination pagination) {
        this.pagination = pagination;
    }

    public static String getEmptyString(String s) {
           return  s== null ? "": s;
    }
    public static Integer getEmptyInteger(Integer s) {
           return  s == null ? 0: s;
    }


}
